# Written on December 2008 by Giorgio Marciano' <ledzgio@writeme.com>
# a simple app that:
# a. well format each directory under source directory to destination directory
# b. also convert each mp3 file in source directory to open source ogg format
#
# GUI module that contains the application user interface
#
# GPL'd for her pleasure..
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

import os
import time
import threading

try:
    import pygtk
    pygtk.require("2.0")
except:
    pass
try:
    import gtk
    import gtk.glade
except:
    sys.exit(1)

class StarUtilxGUI:

    def __init__(self):
        self.gladefile = "StarUtilx.glade"
        #Set the Glade file
        self.wTree = gtk.glade.XML(self.gladefile)
        
    #def start_GUI(self):
    def start_GUI(self, core):
        self.core = core
        self.app_name = "StarUtilx v. 0.0.1"
        #Get the Main Window, and connect the "destroy" event
        self.window = self.wTree.get_widget("main_window").show()
        if (self.window):
            self.window.connect("destroy", gtk.main_quit)
            
        #Create our dictionay and connect it
        dic = { "button_execute" : self.execute, "exit" : gtk.main_quit,\
             "destroy" : gtk.main_quit, "clear_window" : self.clear_window,\
             "button_clear" : self.clear_window, "clear_log" : self.clear_log,\
             "save_log" : self.save_log}     
                
        self.wTree.signal_autoconnect(dic)
        self.write_statusbar(self.app_name)
        
        self.wTree.get_widget("log_text_view").set_overwrite(False)
        
        #setting current folder
        self.wTree.get_widget("source_chooser").set_current_folder(os.path.expanduser("~"))
        self.wTree.get_widget("dest_chooser").set_current_folder(os.path.expanduser("~"))
        
        #show main window        
        gtk.main()
            
    def execute(self, widget):
        #clear the textview
        self.clear_log(widget)
        
        #invoke the core exec method
        self.core.exec_format()
        
    def write_statusbar(self,message):
        label = self.wTree.get_widget("statusbar")
        context = label.get_context_id("statusbar")
        label.push(context,message)

    #write log to the textview
    def write_log(self, log):
        self.log = "["+time.strftime("%H:%M:%S", time.localtime()) + "] " + log + "\n"
        text_buffer = self.wTree.get_widget("log_text_view").get_buffer()
        end_iter = text_buffer.get_end_iter()
        text_buffer.insert(end_iter, self.log)
        self.wTree.get_widget("log_text_view").scroll_to_mark(text_buffer.get_insert(), 0)
               
    def clear_log (self, widget):
        self.wTree.get_widget("log_text_view").get_buffer().set_text("")
        
    def save_log(self, widget):
        self.writeLog("Save functionality not yet implemented")
    
    def clear_window(self, widget):
        print "not implemented yet"